using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentAddressTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentAddressTable objects.
    /// </summary>
    public class StudentAddressTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentAddressTable from the underlying datasource.
        /// </summary>
        /// <param name="studentAddressKey">The unique StudentAddressKey of the StudentAddressTable in the database.</param>
        /// <returns>An StudentAddressTable when the StudentAddressKey was found in the database, or null otherwise.</returns>
        public static StudentAddressTable GetItem(int studentAddressKey)
        {
            StudentAddressTable myStudentAddressTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAddressTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@studentAddressKey", studentAddressKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentAddressTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentAddressTable;
            }
        }


        /// <summary>
        /// Gets an instance of StudentAddressTable from the underlying datasource.
        /// </summary>
        /// <param name="UserAccountKey">The unique UserAccountKey of the StudentAddressTable in the database.</param>
        /// <returns>An StudentAddressTable when the UserAccountKey was found in the database, or null otherwise.</returns>
        public static StudentAddressTable GetItemByUserAccountKey(int UserAccountKey)
        {
            StudentAddressTable myStudentAddressTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAddressTableSelectSingleItemByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentAddressTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentAddressTable;
            }
        }

        /// <summary>
        /// Gets an instance of StudentAddressTable from the underlying datasource.
        /// </summary>
        /// <param name="UserAccountKey">The unique UserAccountKey of the StudentAddressTable in the database.</param>
        /// <returns>An StudentAddressTable when the UserAccountKey was found in the database, or null otherwise.</returns>
        public static StudentAddressTable GetItemByUserAccountKey(int UserAccountKey, Int16 AddressTypeCode)
        {
            StudentAddressTable myStudentAddressTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAddressTableSelectSingleItemByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);
                    myCommand.Parameters.AddWithValue("@AddressTypeCode", AddressTypeCode);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentAddressTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentAddressTable;
            }
        }

        /// <summary>
        /// Returns a list with StudentAddressTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentAddressTable objects.</returns>
        public static StudentAddressTableList GetList()
        {
            StudentAddressTableList tempList = new StudentAddressTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAddressTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StudentAddressTable in the database.
        /// </summary>
        /// <param name="myStudentAddressTable">The StudentAddressTable instance to save.</param>
        /// <returns>The new StudentAddressKey if the StudentAddressTable is new in the database or the existing StudentAddressKey when an item was updated.</returns>
        public static int Save(StudentAddressTable myStudentAddressTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAddressTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentAddressTable.StudentAddressKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@studentAddressKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@studentAddressKey", myStudentAddressTable.StudentAddressKey);
                    }
                    if (myStudentAddressTable.UserAccountKey <= null)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", myStudentAddressTable.UserAccountKey);
                    }
                    if (string.IsNullOrEmpty(myStudentAddressTable.UnitNo))
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", myStudentAddressTable.UnitNo);
                    }
                    if (string.IsNullOrEmpty(myStudentAddressTable.StreetName))
                    {
                        myCommand.Parameters.AddWithValue("@streetName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@streetName", myStudentAddressTable.StreetName);
                    }
                    if (myStudentAddressTable.CityCode <= null)
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", myStudentAddressTable.CityCode);
                    }
                    if (myStudentAddressTable.DistrictCode <= null)
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", myStudentAddressTable.DistrictCode);
                    }
                    if (myStudentAddressTable.TownshipCode <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", myStudentAddressTable.TownshipCode);
                    }
                    if (myStudentAddressTable.CountryCode <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", myStudentAddressTable.CountryCode);
                    }
                    if (string.IsNullOrEmpty(myStudentAddressTable.PostalCode))
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", myStudentAddressTable.PostalCode);
                    }
                    if (myStudentAddressTable.AddressTypeCode <= null)
                    {
                        myCommand.Parameters.AddWithValue("@addressTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@addressTypeCode", myStudentAddressTable.AddressTypeCode);
                    }

                    myCommand.Parameters.AddWithValue("@sameAsResident", myStudentAddressTable.SameAsResident);

                    if (myStudentAddressTable.Status <= null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myStudentAddressTable.Status);
                    }
                    //if (myStudentAddressTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myStudentAddressTable.CreatedDateTime);
                    //}
                    //if (myStudentAddressTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentAddressTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentAddressTable from the database.
        /// </summary>
        /// <param name="studentAddressKey">The StudentAddressKey of the StudentAddressTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int studentAddressKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAddressTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@studentAddressKey", studentAddressKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentAddressTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentAddressTable FillDataRecord(IDataRecord myDataRecord)
        {
            StudentAddressTable myStudentAddressTable = new StudentAddressTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StudentAddressKey")))
            {
                myStudentAddressTable.StudentAddressKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StudentAddressKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
            {
                myStudentAddressTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UnitNo")))
            {
                myStudentAddressTable.UnitNo = myDataRecord.GetString(myDataRecord.GetOrdinal("UnitNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StreetName")))
            {
                myStudentAddressTable.StreetName = myDataRecord.GetString(myDataRecord.GetOrdinal("StreetName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
            {
                myStudentAddressTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
            {
                myStudentAddressTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipCode")))
            {
                myStudentAddressTable.TownshipCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TownshipCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
            {
                myStudentAddressTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PostalCode")))
            {
                myStudentAddressTable.PostalCode = myDataRecord.GetString(myDataRecord.GetOrdinal("PostalCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AddressTypeCode")))
            {
                myStudentAddressTable.AddressTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AddressTypeCode"));
            }

            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SameAsResident")))
            {
                myStudentAddressTable.SameAsResident = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("SameAsResident"));
            }

            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStudentAddressTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentAddressTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentAddressTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentAddressTable;
        }
    }

}
